(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.passwordPolicy.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'passwordPolicyService','storageService', 'commonService'];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, passwordPolicyService, storageService, commonService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("passwordPolicyList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.setupDataStatusOptions = commonService.setupDataStatusOptions();
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.search();
		};
		
		ctrl.copyDefault = function(){
			authService.check().then(function(response){
				if(response.data==true){
					modalService.openErrorDialogs({
					    messages : [ {msg : 'js.passwordpolicy.error.data-exist', args : [storageService.getUserInfo().siteCode]} ]
					});
					return;
				}else{
					authService.copyDefault().then(function(response) {
						ctrl.init();
					}, angular.noop);
				}
		    });
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.passwordPolicy.view', ctrl.selectedRows[0]);
		};

		ctrl.create = function() {
			$scope.go('cda.passwordPolicy.create');
		};

		ctrl.update = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.passwordPolicy.update', ctrl.selectedRows[0]);
		};

		ctrl.delete = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.passwordPolicy.delete', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			ctrl.siteCode = storageService.getUserInfo().siteCode;
			passwordPolicyService.getValidationDatas().then(function(response){
				ctrl.isExistCurrent = response.isExistCurrent;
				ctrl.isEdit = response.isEdit;
				authService.search(ctrl.searchCriteria).then(function(response) {
					angular.merge(ctrl.searchCriteria, response.data);
					gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
				},angular.noop);
				
			});
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);